package com.example.rabbitorderconsumer.consumer;

import com.example.rabbitorderconsumer.bean.OrderBean;
import com.example.rabbitorderconsumer.dao.OrderDao;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Optional;

/**
 * @ClassName OrderConsumer
 * @Description TODO
 * @Auth ying.xiao
 * @Date 2021/6/29
 * @Version 1.0.0
 */
@Component
public class OrderConsumer {

    @Autowired
    private OrderDao orderDao;

//  @RabbitListener(queues = "q.go")
//    public void orderListen(String message){
//        System.out.println("接收到订单创建"+message);
//        //创建订单
//        OrderBean orderBean = new OrderBean();
//        orderBean.setOrderNo(message);
//        orderBean.setStatus(1);
//        orderDao.save(orderBean);
//    }

    @RabbitListener(queues = "q.go.dlx")
    public void DieQueue(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, @Payload String message) throws IOException {
        System.out.println("queue.dlx，获取到消息："+message);

        OrderBean orderBean1 = new OrderBean();
        orderBean1.setOrderNo(message);
        Optional<OrderBean> one = orderDao.findOne(Example.of(orderBean1));
        OrderBean orderBean = one.get();
        //订单超时
        orderBean.setStatus(2);
        //更新订单超时
        orderDao.saveAndFlush(orderBean);
    }

}
